﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CompanyManagementDLL.MetaData;

namespace CompanyManagementDLL.DLL
{
    public class ProjectAssignDLL
    {
        private readonly CompanyMgtEntities db = new CompanyMgtEntities();

        #region Get Assign List

        /// <summary>
        /// Get Project assign list
        /// </summary>
        /// <returns></returns>
        public List<ProjectAssignMetaData> getProjectAssignList()
        {
            List<ProjectAssignMetaData> list = new List<ProjectAssignMetaData>();
            try
            {
                list = db.ProjectAssigns.Select(pa => new ProjectAssignMetaData
                {
                    ID = pa.ID,
                    AssignID = pa.AssignID,
                    ProjectID = pa.ProjectID
                }).ToList();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return list;
        }

        /// <summary>
        /// Get Project assign list
        /// </summary>
        /// <returns></returns>
        public List<ProjectAssignMetaData> getProjectAssignListByProjectID(int ProjectID)
        {
            List<ProjectAssignMetaData> list = new List<ProjectAssignMetaData>();
            try
            {
                list = db.ProjectAssigns.Where(pa => pa.ProjectID == ProjectID).Select(pa => new ProjectAssignMetaData
                {
                    ID = pa.ID,
                    AssignID = pa.AssignID,
                    ProjectID = pa.ProjectID
                }).ToList();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return list;
        }

        /// <summary>
        /// Return list of users avilable in project
        /// </summary>
        /// <param name="ProjectID"></param>
        /// <returns></returns>
        public List<ProjectMetaData> getProjectAssignUserListByProjectID(int ProjectID)
        {
            List<ProjectMetaData> list = new List<ProjectMetaData>();
            try
            {
                list = db.GetProjectList(ProjectID).Where(pa => pa.PorjectID == ProjectID).Select(u => new ProjectMetaData
                {
                    ProjectName = u.ProjectName,
                    PorjectID = u.PorjectID,
                    StartDate = u.StartDate,
                    EndDate = u.EndDate,
                    CreatedBy = u.CreatedBy,
                    ModifyBy = u.ModifyBy,
                    ModifyDate = u.ModifyDate,
                    CreatedDate = u.CreatedDate,
                    IsActive = u.IsActive ?? false,
                    IsArchive = u.IsArchive,
                    Description = u.Description,
                    UserName=u.UserName
                }).ToList();
            }
            catch (Exception ex)
            {

                throw ex;
            }
            return list;
        }

    
        #endregion

        #region DML Opeartion


        /// <summary>
        /// insert assign entity
        /// </summary>
        /// <param name="rolemetadata"></param>
        public void InsertAssign(ProjectAssignMetaData assignmetadata)
        {
            ProjectAssign tblAssign = new ProjectAssign();
            try
            {
                tblAssign.ProjectID = assignmetadata.ProjectID;
                tblAssign.AssignID = assignmetadata.AssignID;
                db.ProjectAssigns.Add(tblAssign);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// delete assign entity
        /// </summary>
        /// <param name="id"></param>
        public void DeleteAssign(int id)
        {
            try
            {
                ProjectAssign tblAssign = new ProjectAssign();
                tblAssign = db.ProjectAssigns.Where(r => r.ID == id).FirstOrDefault();
                db.ProjectAssigns.Remove(tblAssign);
                db.SaveChanges();
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        #endregion


    }
}
